package org.skydiveapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RigComponentEditActivity extends Activity
{
	// requests
	private static final int REQUEST_NOTES = 0;
	
	// data
	private String notes = "";

	// listeners
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			Intent intent = new Intent(RigComponentEditActivity.this, NotesEditActivity.class);
			intent.putExtra(Constants.EXTRA_NOTES, notes);
			startActivityForResult(intent, REQUEST_NOTES);
		}
	};
	private OnClickListener deleteListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			delete();
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		// init ui
		initializeUI();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		if (requestCode == REQUEST_NOTES && resultCode == RESULT_OK)
		{
			String notes = intent.getStringExtra(Constants.EXTRA_NOTES);
			updateNotes(notes);
		}
	}
	
	private void initializeUI()
	{
		// show view
		setContentView(R.layout.rig_component_edit_view);
		
		// get data
		Bundle extras = getIntent().getExtras();
		boolean edit = (extras != null && extras.getBoolean(Constants.EXTRA_RIG_COMPONENT_IS_EDIT, false));
		String name = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_NAME)) ?
				extras.getString(Constants.EXTRA_RIG_COMPONENT_NAME) : "";
		String serialNumber = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER)) ?
				extras.getString(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER) : "";
		notes = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_NOTES)) ?
				extras.getString(Constants.EXTRA_RIG_COMPONENT_NOTES) : "";
		
		// set header text
		TextView headerTextView = (TextView)findViewById(R.id.EditHeaderTextView);
		headerTextView.setText(R.string.rig_component_header);
		
		// set name
		EditText nField = (EditText)findViewById(R.id.RigComponentNameField);
		nField.setText(name);
		
		// set serial number
		EditText snField = (EditText)findViewById(R.id.RigComponentSerialNumberField);
		snField.setText(serialNumber);
		
		// set notes
		TextView notesField = (TextView)findViewById(R.id.RigComponentNotesField);
		notesField.setOnClickListener(notesEditListener);
		notesField.setText(notes);
		
		// add button listeners	
		Button deleteButton = (Button)findViewById(R.id.RigComponentDeleteButton);
		deleteButton.setOnClickListener(deleteListener);
		deleteButton.setVisibility(edit ? View.VISIBLE : View.GONE);
	}
	
	private void updateNotes(String notes)
	{
		this.notes = notes;
		
		// update UI
		TextView notesField = (TextView)findViewById(R.id.RigComponentNotesField);
		notesField.setText(notes);
	}

	private void save()
	{
		// get name from field
		EditText nField = (EditText)findViewById(R.id.RigComponentNameField);
		String name = nField.getText().toString();
		
		// get serial number from field
		EditText snField = (EditText)findViewById(R.id.RigComponentSerialNumberField);
		String serialNumber = snField.getText().toString();
		
		// set result
		Intent result = new Intent();
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_NAME, name);
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER, serialNumber);
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_NOTES, notes);
		
		setResult(RESULT_OK, result);
		
		// finish
		finish();
	}
	
	private void delete()
	{
		Intent result = new Intent();
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_DELETE, true);
		setResult(RESULT_OK, result);
		
		// finish
		finish();
	}
	
	@Override
	public void onBackPressed()
	{
		save();
		super.onBackPressed();
	}
}
